<template>
  <!-- 财务  财务账单 打款申请 -->
  <div class="app-container jklis" v-loading="loading">
    <el-form :model="queryParams" ref="queryForm" :inline="true" class="zdy-form search-form-new" label-width="100px">
      <!-- 
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型" size="small" clearable>
          <el-option v-for="(dict, key) in commonMaps.tenantBillOrderTypeMap" :key="key" :label="dict" :value="key" />
        </el-select>
      </el-form-item>
      <el-form-item label="账单类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择账单类型" size="small" clearable>
          <el-option v-for="(dict, key) in commonMaps.billTypeMap" :key="key" :label="dict.label" :value="key" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="支付宝账户" prop="account">
        <el-select v-model="queryParams.account" placeholder="请选择支付宝账户" size="small" clearable>
          <el-option v-for="(account, key) in ZfbList" :key="key" :label="account" :value="account" />
        </el-select>
      </el-form-item>
      <el-form-item label="日期" prop="">
        <el-date-picker v-model="timer" type="daterange" :picker-options="pickerOptions" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item class="btns">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="searchList" class="zdy-btn">搜索</el-button>
        <span class="tip" style="margin-left: 20px;color: red;">
          请联系商务添加支付宝账户！
        </span>
        <!-- <el-button icon="el-icon-refresh" type="primary" plain size="mini" @click="resetQuery" class="zdy-btn zdy-btn-refresh">重置</el-button> -->
      </el-form-item>
    </el-form>
    <div class="table_box">
      <el-table :data="tableData.records" border stripe>
        <!-- <el-table-column prop="id" label="ID" width="55"></el-table-column> -->
        <el-table-column prop="alipayOrderNo" label="支付宝单号" width="300"></el-table-column>
        <el-table-column prop="transDt" label="时间"></el-table-column>
        <!-- <el-table-column prop="orderType" label="订单类型" width="110">
          <template slot-scope="scope">
            <div>
              {{ commonMaps.tenantBillOrderTypeMap[scope.row.orderType] }}
            </div>
          </template>
        </el-table-column> -->
        <el-table-column prop="direction" label="类型" width="100">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.direction=='收入'">收入</el-tag>
            <el-tag type="danger" v-if="scope.row.direction=='支出'">支出</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="transAmount" label="金额" width="100"></el-table-column>
        <el-table-column prop="balance" label="当前余额"></el-table-column>
        <el-table-column prop="otherAccount" label="对方账户" show-overflow-tooltip></el-table-column>
        <el-table-column prop="type" label="记账类型" width="120"></el-table-column>
        <el-table-column prop="transMemo" label="账务备注" show-overflow-tooltip></el-table-column>
      </el-table>
    </div>
    <div class="pages-box">
      <pagination v-show="tableData.total > 0" :total="tableData.total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    </div>
  </div>
</template>

<script>
import billListApi from "@/api/financeCenter/billList.js";
export default {
  name: "ZfbBillpage",
  components: {},
  data() {
    return {
      pickerOptions: {
        onPick: ({ minDate }) => {
          // 用户选择第一个日期后，记录时间戳（用于计算30天范围）
          if (minDate) {
            this.limitRange = minDate.getTime();
          } else {
            this.limitRange = null;
          }
        },
        disabledDate: (time) => {
          // 1. 限制可选范围为最近一年
          const oneYearAgo = new Date();
          oneYearAgo.setFullYear(oneYearAgo.getFullYear() - 1);
          if (
            time.getTime() > Date.now() ||
            time.getTime() < oneYearAgo.getTime()
          ) {
            return true;
          }

          // 2. 如果已选择第一个日期，限制最大区间为30天
          if (this.limitRange) {
            const day30 = 30 * 24 * 3600 * 1000;
            const maxRange = this.limitRange + day30;
            const minRange = this.limitRange - day30;
            return time.getTime() > maxRange || time.getTime() < minRange;
          }

          return false;
        },
      },
      limitRange: null, // 存储用户选择的第一个日期的时间戳
      timer: [],
      // 提号时间，商品ID 游戏类型 区服 实名状态   游戏账号【对应的商品链接】
      tableData: {
        records: [],
        total: 0,
      },
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        startTime: "",
        endTime: "",
        account:''
      },
      ZfbList:[]
    };
  },
  created() {
    this.get30Day()
    this.getZfbList()
    // this.getList();
  },
  computed: {
    userInfo() {
      return this.$store.getters.userInfo;
    },
    shopInfo() {
      return this.$store.getters.shopInfo;
    }
  },
  methods: {
    // 默认最近三十天
    get30Day() {
      const endDate = new Date(); // 今天
      const startDate = new Date();
      startDate.setDate(endDate.getDate() - 29); // 30天区间（含今天）
      const start = this.parseTime(startDate, "{y}-{m}-{d}");
      const end = this.parseTime(endDate, "{y}-{m}-{d}");
      this.timer = [start, end];
    },
    // 获取支付宝账号列表
    getZfbList(){
      // /tenantPushSettings/getSome
      billListApi.getZfbList({
        tenantId: this.shopInfo.tenantId,
        type: 4, //4支付宝管理
      }).then(res=>{
        this.ZfbList = res.data
        if(this.ZfbList&&this.ZfbList.length){
          this.queryParams.account = this.ZfbList[0]
          this.getList()
        }else{
          this.$alert('您未配置支付宝账户，请联系商务开通支付宝流水接口！')
        }
      })
    },
    // 获取列表
    getList() {
      // queryParams 请求列表
      const obj = {
        pageNo: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize,
        startTime: "",
        endTime: "",
        account:this.queryParams.account
      };
      if (this.timer && this.timer.length == 2) {
        obj.startTime = this.timer[0] + " 00:00:00";
        obj.endTime = this.timer[1] + " 23:59:59";
      }else{
        return this.$message.error('请选择日期范围！')
      }
      if(!obj.account){
        return this.$message.error('请选择支付宝账户！')
      }
      this.loading = true;
      billListApi.getZFBbillList(obj).then((res) => {
        this.loading = false;
        if (res.code == 200) {
          const zfbData = JSON.parse(res.msg);
          if (zfbData.msg == "Success") {
            this.tableData.records = zfbData.detailList;
            this.tableData.total = Number(zfbData.totalSize);
          } else {
            this.$message.error(zfbData.subMsg);
          }
        }
      }).catch(err=>{
        this.loading = false;
      })
    },
    // 搜索
    searchList() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.get30Day()
      this.queryParams = {
        pageNum: 1,
        pageSize: 20,
        startTime: "",
        endTime: "",
      };
      this.getList();
    },
  },
};
</script>
<style lang="scss" scoped>
</style>
